//
//  JKRemoteResult.m
//  shopcity
//
//  Created by huangbenhua on 15-4-11.
//  Copyright (c) 2015年 laohuang. All rights reserved.
//

#import "JKRemoteResult.h"

@interface JKRemoteResult()
{
    UBTargetCaller* _caller;
    JKResponse* _resp;
}

@end

@implementation JKRemoteResult

-(JKResponse*)response
{
    return _resp;
}

-(NSDictionary*)content
{
    if (_resp && _resp.content) {
        return _resp.content;
    }
    return @{};
}

-(id)setCaller:(UBTargetCaller*)caller
{
    _caller = caller;
    return self;
}
+(id)target:(id)target action:(SEL)action
{
    if (!target) {
        return nil;
    }
    JKRemoteResult* call = [[JKRemoteResult alloc] init];
    return [call setCaller:[UBTargetCaller of:target action:action]];
};
+(id)target:(id)target action:(SEL)action tag:(id)tag
{
    return [[self target:target action:action] bindTag:tag];
};
+(id)target:(id)target action:(SEL)action info:(id)info
{
    return [[self target:target action:action] bindInfo:info];
};

-(BOOL)OK
{
    if (_resp) {
        return _resp.OK;
    }
    return NO;
}

-(BOOL)success
{
    if (_resp) {
        return _resp.success;
    }
    return YES;
}

-(BOOL)failure
{
    if (_resp) {
        return _resp.failure;
    }
    return YES;
}

-(BOOL)isTail
{
    if (_resp) {
        return [_resp isTail];
    }
    return YES;
}

-(BOOL)hasMore
{
    if (_resp) {
        return [_resp hasMore];
    }
    return NO;
}

-(JKRemoteResult*)bindTag:(id)tag
{
    self.tag = tag;
    return self;
};
-(JKRemoteResult*)bindInfo:(id)info
{
    self.info = info;
    return self;
};

-(void)call:(JKResponse*)resp
{
    _resp = resp;
    if (_caller) {
        [_caller call:self];
    }
}



-(id)objectForKey:(NSString*)key
{
    if (_resp) {
        return [_resp objectForKey:key];
    }
    return nil;
};
-(id)objectForKey:(NSString*)key nilValue:(id)val
{
    if (_resp) {
        return [_resp objectForKey:key nilValue:val];
    }
    return val;
};

-(NSDictionary*)dictForKey:(NSString*)key
{
    if (_resp) {
        return [_resp dictForKey:key];
    }
    return @{};
};
-(NSArray*)arrayForKey:(NSString*)key
{
    if (_resp) {
        return [_resp arrayForKey:key];
    }
    return @[];
};
-(NSString*)stringForKey:(NSString*)key
{
    if (_resp) {
        return [_resp stringForKey:key];
    }
    return @"";
};
-(NSNumber*)numberForKey:(NSString*)key
{
    if (_resp) {
        return [_resp numberForKey:key];
    }
    return @0;
};

-(NSString*)stringForKey:(NSString*)key nilValue:(NSString*)val
{
    if (_resp) {
        return [_resp stringForKey:key nilValue:val];
    }
    return val;
};
-(NSNumber*)numberForKey:(NSString*)key nilValue:(NSNumber*)val

{
    if (_resp) {
        return [_resp numberForKey:key nilValue:val];
    }
    return val;
}

@end

